﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using App.Model;

namespace App.Data.SqlServer {
    public partial class AppDbContext : DbContext, IUnitOfWork {
        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            SetupJobPositionEntity(modelBuilder);
            SetupCompanyEntity(modelBuilder);
            SetupRecruiterEntity(modelBuilder);
            SetupApplicationEntity(modelBuilder);
            SetupBulletinEntity(modelBuilder);
            SetupCandidateEntity(modelBuilder);
            SetupCertificationEntity(modelBuilder);
            SetupEducationEntity(modelBuilder);
            SetupJobCategoryEntity(modelBuilder);
            SetupNewsEntity(modelBuilder);
            SetupPictureEntity(modelBuilder);
            SetupResumeEntity(modelBuilder);
            SetupSkillEntity(modelBuilder);
            SetupTagEntity(modelBuilder);
            SetupTrainingEntity(modelBuilder);
            SetupLovEntity(modelBuilder);
        }
        private static void SetupJobPositionEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<JobPosition>().ToTable("nc_jobposition");
            modelBuilder.Entity<JobPosition>().HasKey(c => c.Id);

            modelBuilder.Entity<JobPosition>().HasMany(i => i.Applications).WithRequired(i => i.JobPosition).HasForeignKey(i=>i.JobPositionId);

            modelBuilder.Entity<JobPosition>().HasRequired(i => i.ProvinceLov).WithRequiredPrincipal();
            modelBuilder.Entity<JobPosition>().HasRequired(i => i.CityLov).WithRequiredPrincipal();

            //SetupJobPositionEntity(modelBuilder);
        }
        public DbSet<JobPosition> JobPositions { get; set; }

        private static void SetupCompanyEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Company>().ToTable("nc_company");
            modelBuilder.Entity<Company>().HasKey(c => c.Id);
        }
        public DbSet<Company> Companies { get; set; }

        private static void SetupRecruiterEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Recruiter>().ToTable("nc_recruiter");
            modelBuilder.Entity<Recruiter>().HasKey(c => c.Id);
        }
        public DbSet<Recruiter> Recruiters { get; set; }

        private static void SetupApplicationEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Application>().ToTable("nc_application");
            modelBuilder.Entity<Application>().HasKey(c => c.Id);

            modelBuilder.Entity<Application>().HasRequired(i => i.Resume).WithMany().HasForeignKey(i => i.ResumeId);
            modelBuilder.Entity<Application>().HasRequired(i => i.JobPosition).WithMany().HasForeignKey(i=>i.JobPositionId);
                
        }
        public DbSet<Application> Applications { get; set; }

        private static void SetupBulletinEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Bulletin>().ToTable("nc_bulletin");
            modelBuilder.Entity<Bulletin>().HasKey(c => c.Id);
        }
        public DbSet<Bulletin> Bulletins { get; set; }

        private static void SetupCandidateEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Candidate>().ToTable("nc_candidate");
            modelBuilder.Entity<Candidate>().HasKey(c => c.Id);
        }
        public DbSet<Candidate> Candidates { get; set; }

        private static void SetupCertificationEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Certification>().ToTable("nc_certification");
            modelBuilder.Entity<Certification>().HasKey(c => c.Id);
        }
        public DbSet<Certification> Certifications { get; set; }

        private static void SetupEducationEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Education>().ToTable("nc_education");
            modelBuilder.Entity<Education>().HasKey(c => c.Id);
        }
        public DbSet<Education> Educations { get; set; }

        private static void SetupJobCategoryEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<JobCategory>().ToTable("nc_jobcategory");
            modelBuilder.Entity<JobCategory>().HasKey(c => c.Id);
        }
        public DbSet<JobCategory> JobCategorys { get; set; }

        private static void SetupNewsEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<News>().ToTable("nc_news_r");
            modelBuilder.Entity<News>().HasKey(c => c.Id);
        }
        public DbSet<News> Newss { get; set; }



        private static void SetupPictureEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Picture>().ToTable("nc_picture");
            modelBuilder.Entity<Picture>().HasKey(c => c.Id);
        }
        public DbSet<Picture> Pictures { get; set; }

        private static void SetupResumeEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Resume>().ToTable("nc_resume");
            modelBuilder.Entity<Resume>().HasKey(c => c.Id);
        }
        public DbSet<Resume> Resumes { get; set; }

        private static void SetupSkillEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Skill>().ToTable("nc_skill");
            modelBuilder.Entity<Skill>().HasKey(c => c.Id);
        }
        public DbSet<Skill> Skills { get; set; }

        private static void SetupTagEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Tag>().ToTable("nc_tag");
            modelBuilder.Entity<Tag>().HasKey(c => c.Id);
        }
        public DbSet<Tag> Tags { get; set; }

        private static void SetupTrainingEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Training>().ToTable("nc_training");
            modelBuilder.Entity<Training>().HasKey(c => c.Id);
        }
        public DbSet<Training> Trainings { get; set; }

        private static void SetupWorkEntity(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Work>().ToTable("nc_work");
            modelBuilder.Entity<Work>().HasKey(c => c.Id);
        }
        public DbSet<Work> Works { get; set; }

        private static void SetupLovEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Lov>().ToTable("sys_Lov");
            modelBuilder.Entity<Lov>().HasKey(c => c.Id);
        }
        public DbSet<Lov> Lovs { get; set; }

        /// <summary>
        /// Allows saving changes via the IUnitOfWork interface.
        /// </summary>
        void IUnitOfWork.SaveChanges() {
            base.SaveChanges();
        }
    }
}
